package com.cq.hd.order.api.vo;

import com.alibaba.fastjson.annotation.JSONField;
import com.cq.hd.common.enums.biz.RewardTypeEnum;
import com.cq.hd.common.serialize.Decimal2Serializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 购买订单详情（消费者查看）
 */
@Data
public class AppOrderDetailVo implements Serializable {

    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "商家id")
    private Long businessId;

    private Long agentId;

    private Long agent2Id;

    @ApiModelProperty(value = "活动id")
    private Long activeId;

    @ApiModelProperty(value = "是否是零元购活动（1-不是，2-是）")
    private Integer zeroState;

    @ApiModelProperty(value = "订单号")
    private String orderNo;

    @ApiModelProperty(value = "订单类型(1-团购订单,2-报名订单,4-团购+报名订单,5积分订单)")
    private Integer orderType;

    @ApiModelProperty(value = "活动标题")
    private String activeTitle;

    @ApiModelProperty(value = "活动图片")
    private String activeImg;

    @ApiModelProperty(value = "活动开始时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime startTime;

    @ApiModelProperty(value = "活动结束时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime endTime;

    @ApiModelProperty(value = "活动有效开始时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime validStartTime;

    @ApiModelProperty(value = "活动有效结束时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime validEndTime;

    @ApiModelProperty(value = "补贴类型(0-未设置,1-自动按比例分摊,2-手动设置)")
    private Integer subsidyState;

    @ApiModelProperty(value = "多件多折状态(1-关闭，2-开启)")
    private Integer discountState;

    @ApiModelProperty(value = "是否是阶梯价格(1-否，2-是)")
    private Integer stepPriceState;

    @ApiModelProperty(value = "分销开启状态(1-关闭，2-开启)")
    private Integer agentState;

    @ApiModelProperty(value = "核销方式(1-线上核销，2-线下核销)")
    private Integer verifyMethod;

    @ApiModelProperty(value = "未满员是否自动退款:1-是，2-否")
    private Integer noFullAutoRefundState;

    @ApiModelProperty(value = "活动总库存")
    private Integer totalStock;

    @ApiModelProperty(value = "订单状态(1-待支付,2-待核销,3-核销中,4-已取消,5-已完成,6-已关闭)")
    private Integer orderState;

    @ApiModelProperty(value = "下单数量")
    private Integer orderNum;


    @ApiModelProperty(value = "活动价格(如果是报名活动的阶梯价格，则是阶梯价格的最大价格)")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal activePrice;


    @ApiModelProperty(value = "订单总额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal orderPrice;


    @ApiModelProperty(value = "实付金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal payPrice;


    @ApiModelProperty(value = "红包金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal redPacketAmt;


    @ApiModelProperty(value = "优惠金额")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal discountAmt;

    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal actualCommissionAmt;

    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal subAgentCommissionAmt;

    @ApiModelProperty(value = "其他权益")
    private String otherEquity;

    @ApiModelProperty(value = "计算佣金类型(0 - 无, 1 - 固定佣金, 2 - 固定佣金比例 ， 3 - 专属佣金比例)")
    private Integer calCommissionType;

    @ApiModelProperty(value = "支付时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime payTime;

    @ApiModelProperty(value = "下单时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime orderTime;

    @ApiModelProperty(value = "订单取消时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime cancelTime;

    @ApiModelProperty(value = "订单取消原因")
    private String cancelReason;

    @ApiModelProperty(value = "订单完成时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime finishTime;

    @ApiModelProperty(value = "订单关闭时间")
    @JSONField(format="yyyy.MM.dd HH:mm:ss")
    private LocalDateTime closeTime;

    @ApiModelProperty(value = "订单备注")
    private String orderNote;


    @ApiModelProperty(value = "下单用户昵称(卖出/分销订单存在)")
    private String orderNickName;

    @ApiModelProperty(value = "下单用户头像(卖出/分销订单存在)")
    private String orderAvatar;

    @ApiModelProperty(value = "经纪人昵称(卖出订单存在)")
    private String agentNickName;

    @ApiModelProperty(value = "经纪人头像(卖出订单存在)")
    private String agentAvatar;


    @ApiModelProperty(value = "推广佣金(卖出/分销订单存在)")
    @JsonSerialize(using = Decimal2Serializer.class)
    private BigDecimal commissionAmt = BigDecimal.ZERO;

    @ApiModelProperty(value = "策划人昵称(分销订单存在)")
    private String businessName;

    @ApiModelProperty(value = "策划人简称")
    private String businessShortName;

    @ApiModelProperty(value = "策划人头像(分销订单存在)")
    private String businessLogo;


    @ApiModelProperty(value = "该活动下单成功人数(购买团购订单存在)")
    private Integer orderUserNum;

    @ApiModelProperty(value = "下单订单项列表(购买订单存在)")
    private List<AppOrderItemVo> appOrderItems;

    @ApiModelProperty(value = "已报名人数，固定报名人数：已有x人报名；阶梯报名人数：已有x人报名，可返y元（没有达到返现标准，就不显示返现）（购买报名订单存在）")
    private String inviteReturnMsg;

    @ApiModelProperty(value = "报名信息（购买报名订单存在）")
    private List<OrderEnrollInfoVo> orderEnrollInfos;

    @ApiModelProperty(value = "活动联系电话（购买报名订单存在）")
    private String activeMobile;

    @ApiModelProperty(value = "活动客服微信号（购买报名订单存在）")
    private String activeWechat;

    @ApiModelProperty(value = "是否需要签到（0-否 1-是）")
    private Integer signInRequired;

    @ApiModelProperty(value = "报名订单项列表")
    private List<AppOrderItemVo> enrollOrderItems;

    @ApiModelProperty(value = "一级经纪人可得积分")
    private BigDecimal agentPointsAmt;

    @ApiModelProperty(value = "二级经纪人可得积分")
    private BigDecimal subAgentPointsAmt;

    @ApiModelProperty(value = "GROUP_BUY(拼团)")
    private String promotionType;

    @ApiModelProperty(value = "订单对应活动的营销类型：GROUP_BUY(拼团)")
    private String activePromotionType;

    @ApiModelProperty(value = "推广奖励类型（0-无，1-佣金，2-其他权益，3-积分）")
    private Integer rewardType = RewardTypeEnum.NONE.getValue();

    @ApiModelProperty(value = "奖励积分")
    private BigDecimal rewardPoints;

    @ApiModelProperty(value = "子单分组")
    private List<OrderItemGroupVo> itemGroups;

    public List<OrderItemGroupVo> getItemGroups() {
        Map<Integer, List<AppOrderItemVo>> enrollItemGroups = Collections.emptyMap();
        Map<Integer, List<AppOrderItemVo>> goodsItemGroups = Collections.emptyMap();
        if (CollectionUtils.isNotEmpty(enrollOrderItems)) {
            enrollItemGroups = enrollOrderItems.stream().collect(Collectors.groupingBy(AppOrderItemVo::getItemGroup));
        }
        if (CollectionUtils.isNotEmpty(appOrderItems)) {
            goodsItemGroups = appOrderItems.stream().collect(Collectors.groupingBy(AppOrderItemVo::getItemGroup));
        }

        Set<Integer> keys = new HashSet<>();
        keys.addAll(enrollItemGroups.keySet());
        keys.addAll(goodsItemGroups.keySet());
        List<OrderItemGroupVo> resultList = new ArrayList<>();
        for (Integer itemGroup : keys) {
            OrderItemGroupVo orderItemGroupVo = new OrderItemGroupVo();
            orderItemGroupVo.setItemGroup(itemGroup == null ? 1 : itemGroup);
            orderItemGroupVo.setEnrollOrderItems(enrollItemGroups.get(itemGroup));
            orderItemGroupVo.setAppOrderItems(goodsItemGroups.get(itemGroup));
            resultList.add(orderItemGroupVo);
        }
        // 按group排序
        resultList.sort(Comparator.comparingInt(OrderItemGroupVo::getItemGroup));

        return resultList;
    }

}
